
SControllers.controller('detail_ctrl', ['$scope', '$http', '$timeout', '$routeParams',
  function($scope, $http, $timeout, $routeParams) {

      $scope.all_plugin_info = [];
      $scope.plugin_info = {};
      $scope.plugin_name = $routeParams.pluginName;

      $scope.disable_map = []; 
      $scope.disable_map[$scope.plugin_name] = "disabled"; 
      $scope.startat_time = "";

      $scope.cron_sim_prefix = "";
      $scope.cron_config = {"seconds":"0", "minutes":"0", "hours":"0", "dayOfMonth":"*", "month":"*", "dayOfWeek":"?"}

      $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd hh:ii', language:'zh-CN'});
      $("#update-info-btn").pin();

      $scope.update_all_plugin_info = function(reload) {
          var url = "/spiderkit/get_plugin_info";
          $http.get(url).success(
              function(data) {
                  $scope.all_plugin_info = data;
                  if (data.length <= 0) {
                      $scope.all_plugin_info = [];
                      window.location.href = "#/";
                      return;
                  }

                  if (reload || ($scope.plugin_name === undefined || $scope.plugin_name === "")) {
                      $scope.plugin_name = data[0]["name"];
                  }

                  $scope.update_plugin_info();
              }
          );
      };

      $(".update-icon > .spinner").hide();

      function updateing() {
          $(".update-icon").removeClass("glyphicon-refresh");
          $(".update-icon > .spinner").show();
      }
      function update_end() {
          $(".update-icon").addClass("glyphicon-refresh");
          $(".update-icon > .spinner").hide();
      }

      $scope.update_plugin_info = function() {
          if ($scope.plugin_name === undefined || $scope.plugin_name === "") {
              return;
          }
          updateing();
          var url = "/spiderkit/get_plugin_info?pluginName="+$scope.plugin_name;
          $http.get(url).success(
              function(data) {
                  update_end();
                  $scope.plugin_info = data[0];
                  if ($scope.plugin_info["isLocalPlugin"]) {
                      $("#delete-plugin-package").hide();
                  }
              }
          ).error(
              function () {
                  update_end();
              }
          );
      };

      $scope.plugin_start_now = function(qualified) {
          var url = "/spiderkit/plugin_manager?action=startNow&pluginName="+$scope.plugin_name+"&pluginQualified="+qualified;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      };
      
      var startat_qualified = "";
      $scope.plugin_start_at = function(qualified) {
          if (qualified !== undefined && qualified !== "") {
              startat_qualified = qualified;
              $scope.startat_time = "";
              $('#startAtModal').modal();
              return;
          }

          if ($scope.startat_time === undefined || $scope.startat_time === "")
              return;

          var url = "/spiderkit/plugin_manager?action=startAt&pluginName="+$scope.plugin_name+"&pluginQualified="+startat_qualified+"&time="+$scope.startat_time;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      }
 
      var startcron_qualified = "";
      $scope.plugin_start_cron = function(qualified) {
          if (qualified !== undefined && qualified !== "") {
              startcron_qualified = qualified;
              $scope.startcron_time = "";
              $('#cronModal').modal();
              return;
          }

          $scope.startcron_time = $scope.cron_config["seconds"] + " " +
                                  $scope.cron_config["minutes"] + " " +
                                  $scope.cron_config["hours"] + " " +
                                  $scope.cron_config["dayOfMonth"] + " " +
                                  $scope.cron_config["month"] + " " +
                                  $scope.cron_config["dayOfWeek"];

          var url = "/spiderkit/plugin_manager?action=launch&pluginName="+$scope.plugin_name+"&pluginQualified="+startcron_qualified+"&cron="+$scope.startcron_time;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      }
       
      $scope.dateUnitTypes = [{
          id:1,
          type :'Minute',
      }, {
          id:2,
          type :'Hour',
      }, {
          id:3,
          type :'Day',
      }];

      $scope.startIncrementalInterval = 0;
      $scope.startIncrementalUnit = "Day";
      var startincremental_qualified = "";
      $scope.plugin_start_incremental = function(qualified) {
          if (qualified !== undefined && qualified !== "") {
              startincremental_qualified = qualified;
              $scope.startat_time = "";
              $('#startIncrementalModal').modal();
              return;
          }

          if ($scope.startIncrementalInterval === 0 || $scope.startIncrementalUnit === "")
              return;

          var url = "/spiderkit/plugin_manager?action=startIncremental&pluginName="+$scope.plugin_name+"&pluginQualified="+startincremental_qualified+"&interval="+$scope.startIncrementalInterval+"&unit="+$scope.startIncrementalUnit;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      }

      var close_qualified = "";
      $scope.plugin_close = function(qualified) {
          if (qualified !== undefined && qualified !== "") {
              close_qualified = qualified;
              $('#closeModal').modal();
              return;
          }
          var url = "/spiderkit/plugin_manager?action=close&pluginName="+$scope.plugin_name+"&pluginQualified="+close_qualified;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      };

      var stop_qualified = "";
      $scope.plugin_stop = function(qualified) {
          if (qualified !== undefined && qualified !== "") {
              stop_qualified = qualified;
              $('#stopModal').modal();
              return;
          }
          var url = "/spiderkit/plugin_manager?action=stop&pluginName="+$scope.plugin_name+"&pluginQualified="+stop_qualified;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(false);
              }
          );
      };

      $scope.plugin_delete = function(qualified) {
          var url = "/spiderkit/plugin_manager?action=delete&pluginName="+$scope.plugin_name;
          $http.get(url).success(
              function(data) {
                  $scope.update_all_plugin_info(true);
              }
          );
      };
      
      $scope.show_panel_plugin_process = function(qualified) {
          var plugin_process_div = document.getElementById("process_"+qualified);
          plugin_process_div.style.display = "block";

          var plugin_process_div = document.getElementById("info_"+qualified);
          plugin_process_div.style.display = "none";
      };

      $scope.show_panel_plugin_info = function(qualified) {
          var plugin_process_div = document.getElementById("process_"+qualified);
          plugin_process_div.style.display = "none";

          var plugin_process_div = document.getElementById("info_"+qualified);
          plugin_process_div.style.display = "block";
      };

      $scope.on_cron_sim_change = function() {
      };

      $scope.update_all_plugin_info(false);
  }]
);


